package com.zy.recursion;

import com.zy.ListNode;

/**
 * @description: 翻转链表递归解法
 * @author: zhaoyi
 * @create: 2019-08-29 22:30
 * <p>
 * https://leetcode-cn.com/explore/orignial/card/recursion-i/257/recurrence-relation/1210/
 **/

public class ReverseList {

    /**
     * 输入: 1->2->3->4->5->NULL
     * 输出: 5->4->3->2->1->NULL
     *
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) {

        if (head == null || head.next == null) {
            return head;
        }
        //递归
        return recursion(null,head);
    }

    private ListNode recursion(ListNode prive, ListNode head) {
        //翻转下head
        if(head.next == null){
           head.next = prive;
           return head;
        }else {
            ListNode nextHead = head.next;
            head.next = prive;
            return recursion(head,nextHead);
        }
    }


}
